import Util from '../../utils/util.js';
import {getMyStat, getIntegralRecord, getStat} from '../../api/member.js';

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false, // 是否加载中
    loadend: false, // 是否加载完毕
    loadTitle: '加载更多内容', // 提示语
    lists: [],
    page: 1,
    et_show: true,

    navs: [
      {title: "完成挑战", amount: 0, icon: "task"},
      {title: "收到的赞", amount: 0, icon: "zan"},
      {title: "全程积分", amount: 0, icon: "integral"},
    ],
    records: [
      {title: "完成 [30张照片中的姜庄]挑战", amount: 120, icon: "", create_time: "2023-01-30  19:00"},
      {title: "获得第 100 个赞", amount: 190, icon: "", create_time: "2023-01-30  19:00"},
      {title: "帮老师打扫卫生  by NPC洋葱", amount: 50, icon: "", create_time: "2023-01-30  19:00"},
      {title: "参与 [Party 策划组] 挑战", amount: 300, icon: "", create_time: "2023-01-30  19:00"},
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;

    Util.chekWxLogin().then(res => {

      wx.showLoading({title: "正在加载..."})
      getMyStat().then(result => {
        wx.hideLoading();
        that.setData({
          member: result.data.member || {}
        })

        let navs = that.data.navs
        if (!Util.isEmpty(result.data.done_task_total)) {
          navs[0].amount = result.data.done_task_total || 0
        }
        if (!Util.isEmpty(result.data.zan_total)) {
          navs[1].amount = result.data.zan_total || 0
        }
        if (!Util.isEmpty(result.data.integral_total)) {
          navs[2].amount = result.data.integral_total || 0
        }
        that.setData({
          navs: navs
        })

      }).catch(errmsg => {
        wx.hideLoading();
        app.Tips({title: errmsg})
      })

      that.getLists()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  getLists() {
    const that = this

    if (that.data.loadend || that.data.loading) {
      return;
    }

    let filter = that.data.filter || {}
    filter['page_num'] = that.data.page;
    that.setData({loading: true, loadTitle: '正在加载...'});
    let lists = that.data.lists || []
    getIntegralRecord(filter).then(res => {
      const list = res.data.items || []
      const loadend = res.data.last_page <= that.data.page;

      lists = app.SplitArray(list, lists);
      that.setData({
        lists: lists,
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '--没有更多了--' : '正在加载...',
        total: res.data.total,
        page: that.data.page + 1,
        et_show: lists.length > 0
      });

      wx.stopPullDownRefresh()
    }).catch(errmsg => {
      wx.stopPullDownRefresh()
      app.Tips({title: errmsg})
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})